﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HdipCricketClub.Models;
using HdipCricketClub.ViewModels;

namespace HdipCricketClub.Models
{
    public class MemberRepository
    {
        CricketClubEntities db = new CricketClubEntities();

        FixtureRepository fixtureRepository = new FixtureRepository();
        
        public Member GetMember(int id)
        {
            return db.Members.SingleOrDefault(m => m.member_id == id);
        }

       
        public IQueryable<Member> FindAllMembers()
        {
            return db.Members.Include("Club").Include("County1").OrderBy(m => m.last_name);
        }

        public Member GetPlayer(int id)
        {
            return db.Members.SingleOrDefault(m => m.member_id == id && m.playing_member == true);
        }
        
        public IQueryable<Member> FindAllPlayers()
        {
            return db.Members.Include("Club").Include("County1").Where(p => p.playing_member == true).OrderBy(p => p.last_name);
        }

        public List<Member> GetPlayersSelectedDetails(List<Member> members)
        {
            List<Member> playersSelected = new List<Member>();

            foreach (Member member in members)
            {
                Member playerSelected = db.Members.Include("Club").Include("County1").Where(m => m.member_id == member.member_id).Single();
                playerSelected.Selected = true;
                playersSelected.Add(playerSelected);
            }

            return playersSelected;

        }

        public List<Member> GetPlayersNotSelectedDetails(List<Member> members)
        {
            List<Member> playersNotSelected = new List<Member>();

            foreach (Member member in members)
            {
                Member playerNotSelected = db.Members.Include("Club").Include("County1").Where(m => m.member_id == member.member_id).Single();
                playersNotSelected.Add(playerNotSelected);
            }

            return playersNotSelected;
        }

        public SelectedPlayersViewModel GetPlayerLists(List<ClubFixturePlayer> currentTeam, int fixtureId)
        {
            
            List<int> currentMemberIds = new List<int>();

            foreach (ClubFixturePlayer player in currentTeam)
            {
                currentMemberIds.Add(player.player_id);
            }

            List<Member> allPlayers = FindAllPlayers().ToList();

            List<Member> playersSelected = new List<Member>();
            List<Member> playersNotSelected = new List<Member>();

            foreach (Member member in allPlayers)
            {
                if (currentMemberIds.Contains(member.member_id))
                {
                    member.Selected = true;
                    playersSelected.Add(member);
                }
                else
                {
                    playersNotSelected.Add(member);
                }
            }

            SelectedPlayersViewModel memberDetails = new SelectedPlayersViewModel();
            memberDetails.SelectedFixture = fixtureRepository.GetFixture(fixtureId);
            memberDetails.PlayersSelected = playersSelected;
            memberDetails.PlayersNotSelected = playersNotSelected;
            memberDetails.noOfPlayersSelected = memberDetails.PlayersSelected.Count();
            memberDetails.noOfPlayersRequired = memberDetails.SelectedFixture.no_of_players;

            return memberDetails;
        }

        public IQueryable<Member> GetPlayersWithStatistics()
        {
            List<int> ClubFixturePlayerIds = db.ClubFixturePlayers.Select(cfp => cfp.player_id).Distinct().ToList();

            List<Member> allMembers = db.Members.OrderBy(m => m.last_name).ToList();

            List<Member> playersWithStatistics = new List<Member>();

            foreach (Member member in allMembers)
            {
                if (ClubFixturePlayerIds.Contains(member.member_id))
                {
                    playersWithStatistics.Add(member);
                }
            }

            return playersWithStatistics.AsQueryable();
        }



    }
}